import java.io.*;
import java.util.*;

import org.gjt.jclasslib.io.*;
import org.gjt.jclasslib.bytecode.*;
import org.gjt.jclasslib.io.ClassFileWriter;
import org.gjt.jclasslib.structures.*;
import org.gjt.jclasslib.structures.attributes.CodeAttribute;
import org.gjt.jclasslib.structures.constants.ConstantDoubleInfo;

public class change_test {
	public void change_test(){};
    public static byte [] Run(byte [] data) {
        try {
/*            String filePath = "D:\\android_ana_tool\\dex2jar-0.0.9.12\\sw测试1\\com\\example\\whsw_test\\test.class";
            FileInputStream fis = new FileInputStream(filePath);//文件->字节流
            //文件字节数
            File file=new File("D:\\android_ana_tool\\dex2jar-0.0.9.12\\sw测试1\\com\\example\\whsw_test\\test.class"); 
            byte[] bytes=new byte[(int)file.length()];
 /*          DataInputStream di = new DataInputStream(fis);
              byte b[]=null;
              di.read(b);
              ByteCodeReader.readByteCode(b);       
              di.close();
              ByteCodeInputStream num=new ByteCodeInputStream(fis);
              int NUM=num.getBytesRead();
              byte[] bytes=new byte[180];//获取文件的byte数   */
 /*           fis.read(bytes);
            List listcode=ByteCodeReader.readByteCode(bytes);
            fis.close();
            
        }
        catch (Exception e)
        {
        	System.out.println("failed!");
        }*/
/////////////////////////////////////////////////////////////////////////////////////////
  /*          String filePath = "D:\\android_ana_tool\\dex2jar-0.0.9.12\\sw测试1\\com\\example\\whsw_test\\test.class";
            FileInputStream fis = new FileInputStream(filePath);//文件->字节流
            DataInput di = new DataInputStream(fis);
            DataOutput datacode=new DataOutputStream(ofis);
            ClassFile cf = new ClassFile();//读取 class文件
            cf.read(di);
            MethodInfo[] methods=cf.getMethods();//获取methods
            AttributeInfo[] att=methods[1].getAttributes();//获取method-》attribute
            CodeAttribute codeinfo=(CodeAttribute)att[0];
            byte[] codes=codeinfo.getCode();//获取method->attribute->code
            List listcode=ByteCodeReader.readByteCode(codes);
            //插入position前的指令
            //AbstractInstruction[] preInstructions=NUll;
            //插入position后的指令
           AbstractInstruction[] postInstructions=new  AbstractInstruction[6];
            for(int i=0;i<=5;i++)
            {
            	
            	postInstructions[i]=(AbstractInstruction)listcode.get(i+9);
            }
            //实例化一个 CodeInsertion
            CodeInsertion codeinsertion=new CodeInsertion(3,null,postInstructions,true);
            List <CodeInsertion>  codeinsertions=new ArrayList<CodeInsertion>();
            codeinsertions.add(codeinsertion);
            //调用CodeInsertion.apply方法 返回新的 instruction List
           List newlistcode= CodeInsertion.apply(listcode,codeinsertions,codeinfo);
           //修改tableSwitch 跳转offset
           TableSwitchInstruction temp=(TableSwitchInstruction)newlistcode.get(3);
    	   temp.setDefaultOffset(43);
    	   int[] offsets={25,31,37};
    	   temp.setJumpOffsets(offsets);
           newlistcode.set(3, temp);
        //修改原branch为 nop
           SimpleInstruction temp2=new SimpleInstruction(0x00);
           for(int i=15;i<=30;i++)
           {
        	   temp2.setOpcode(0x00);
               if(i<21)newlistcode.set(i, temp2);
               if(i>=21)newlistcode.add(temp2);
           }
           BranchInstruction temp3=(BranchInstruction)newlistcode.get(5);
           temp3.setBranchOffset(15);
           BranchInstruction temp4=(BranchInstruction)newlistcode.get(7);
           temp4.setBranchOffset(9);   
           BranchInstruction temp5=(BranchInstruction)newlistcode.get(9);
           temp5.setBranchOffset(3);
           byte[] newcodes=ByteCodeWriter.writeByteCode(newlistcode);
           //新的 code写回method
           codeinfo.setCode(newcodes);
           File newfile=new File(filePath);
           ClassFileWriter.writeToFile(newfile, cf);     
            fis.close();*/
  /////////////////////////////////////////////////////////////////////////////////////////
           //读取文件 
            // filePath = "D:\\android_ana_tool\\dex2jar-0.0.9.12\\sw测试4\\cn\\m\\xys\\GameView.class";
        	//filePath = "D:\\android_ana_tool\\dex2jar-0.0.9.12\\sw测试6\\com\\example\\whsw_test\\test.class";
        	//filePath = "D:\\android_ana_tool\\dex2jar-0.0.9.12\\wh测试1\\com\\example\\antiy_dex\\dex_test.class";
        	/*String dfile=Path;
        	System.out.println(Path+"   start!");
        	filePath=Path;
        	fis = new FileInputStream(Path);//文件->字节流
        	*/
        	////
        	//byte[] data=null;
        	InputStream instream =new ByteArrayInputStream(data);           
        	DataInput di = new DataInputStream(instream);
            ////
        	//DataInput di = new DataInputStream(fis);
        	cf = new ClassFile();//读取 class文件
            cf.read(di);
           //获取methods
            methods=cf.getMethods();//获取methods
            for(int i=0;i<methods.length;i++)//循环调整每个 class 文件中的 method
            {
             	optimization(i);//优化每个方法
            }
            File newfile=new File(filePath);
        	  //////
            ByteArrayOutputStream outstream=new ByteArrayOutputStream();
            DataOutput out = new DataOutputStream(outstream);
            cf.write(out); 
           byte[] outdata=outstream.toByteArray();
           return outdata;
         //  ClassFileWriter.writeToFile(newfile, cf);    
            //关闭文件
         //  fis.close();
        }
        catch (Exception e)
        {
        	//System.out.println("\n"+ Path+"   failed! \n");
        }
       // System.out.println(Path+"    finish!");
		return null;
    }

    static List<String> fileList = new ArrayList<String>();
    static String filePath;//文件路径
    static MethodInfo[] methods;//方法数组
    static ClassFile cf;//class struct
    static CodeAttribute codeinfo;//code
    static FileInputStream fis;//文件流
    static int methodnum;
 public static void optimization(int num) throws Exception//优化方法
 {
	 try
	 {
		 methodnum=num;
	  int flag=0;
	  AttributeInfo[] att=methods[num].getAttributes();//获取method-》attribute
	  for(int i=0;i<att.length;i++)//获取attribute->codeattribute
	  {
		  if("CodeAttribute".equals(att[i].getClass().getSimpleName())) {flag=i;break;}
	  }
      codeinfo=(CodeAttribute)att[flag];
      //过滤expection table
      //codeinfo.setExceptionTable(null);
     //过滤结束
      byte[] codes=codeinfo.getCode();//codeattribute->code bytes
      List listcode=ByteCodeReader.readByteCode(codes);//code byte[]->instructions List便于修改
      //查找 switch结构 并 调整
      search_adjust(listcode);
  	 }
     catch (Exception e)
     {
  	   
  	  // String s1="\n switch-case optimization   "+methods[num].getName()+"method failed \n";
      //	System.out.println(s1);
     }
}
public static void search_adjust(List<AbstractInstruction> listcode) throws Exception //查找 switch结构 并 调整
{
 	int position=0;
	int switch_index=0;//switch语句index
	int switch_endindex=0;//switch语句结束 转向语句的index
	int switch_endoffset=0;//switch语句结束 转向语句的offset
	int offset_position;//switch 语句 offset
	int branch_startindex;
	int branch_startoffset;
	int branch_endindex;//
	int branch_endoffset;
	int default_index;
	int offset_increase;
	int index_temp;
	int offset_temp;
	int index_max=0;
	int index_min=0;
	int index_return=0;
	int offset_return=0;
	int num;//插入object数目
	int j=0;
	 List<Integer> tostart=new ArrayList<Integer>();
     List<Integer> toend=new ArrayList<Integer>();
     List<Integer> tobigend=new ArrayList<Integer>();//向下跳出branch
     List<Integer> tostartindex=new ArrayList<Integer>();
     List<Integer> toendindex=new ArrayList<Integer>();
     List<Integer> tobigendindex=new ArrayList<Integer>();
	///
	int newswitch_index=0;
	int newdefault_index=0;
    int newswitch_endindex=0;
    int newbranch_endindex=0;
    int newbranch_startindex=0;
    int newswitch_endoffset=0;
    List <Integer> direbreak=new ArrayList<Integer>();//记录直接break的 branch
    List <Integer> branchbreak=new ArrayList<Integer>();//记录 branch中跳转到switch结束位置的指令
	try{
		//获取return index
        AbstractInstruction opreturn=(AbstractInstruction)listcode.get(0);
        for(int i=0;i<listcode.size();i++)
         {
        	 opreturn=(AbstractInstruction)listcode.get(i);
        	 if((opreturn.getOpcode()==0xac)||(opreturn.getOpcode()==0xad)||(opreturn.getOpcode()==0xae)||(opreturn.getOpcode()==0xaf)||(opreturn.getOpcode()==0xb0)||(opreturn.getOpcode()==0xb1))
        	 {
        		 index_return=i;
        		 offset_return=opreturn.getOffset();
        	 }
         }
        
	for(int i=0;i<listcode.size();i++)
	{
	    //循环查找TableSwitchInstruction或LookupSwitchInstruction
	    boolean TSI_flag=false;//TableSwitchInstruction 标志
	    boolean LSI_flag=false;//LookupSwitchInstruction 标志
		if("TableSwitchInstruction".equals(listcode.get(i).getClass().getSimpleName())) TSI_flag=true;
	    if("LookupSwitchInstruction".equals(listcode.get(i).getClass().getSimpleName())) LSI_flag=true;
		if(TSI_flag||LSI_flag)
		{
			switch_index=i;
			position=i;//插入点
			/////////////////获取branch语句 准备重新插入
			int default_offset=-1;
			int [] jumpoffset=null;
			offset_position=listcode.get(i).getOffset();//插入点offset
			if(TSI_flag)
			{
				TableSwitchInstruction ts_instruction=(TableSwitchInstruction)listcode.get(switch_index);
				default_offset=ts_instruction.getDefaultOffset();//获取defaultoffset
				jumpoffset=ts_instruction.getJumpOffsets();//获取 jumpoffset offset数组
			}
			if(LSI_flag)
			{
				 LookupSwitchInstruction ls_insrtuction=( LookupSwitchInstruction)listcode.get(switch_index);
				 default_offset=ls_insrtuction.getDefaultOffset();
			     List< MatchOffsetPair> lsi_pair= ls_insrtuction.getMatchOffsetPairs(); //jump Pair
			     ////做一个转化  将matchoffsetPairs问题转化为jumpoffset问题
			     //////将 offset添加到jumpoffset数组中
			     jumpoffset=new int[lsi_pair.size()];  
			     for(int r=0;r<jumpoffset.length;r++)
			     {
			    	 jumpoffset[r]=lsi_pair.get(r).getOffset();
			     }
			}
			List <Integer> branch_index=new ArrayList<Integer>();
			List <Integer> branch_indexnum=new ArrayList<Integer>();
			//int [] branch_index=new int [jumpoffset.length];//branch_index
			default_index=search_index(listcode,default_offset+offset_position);//default_index
			//branchoffset > return offset 才需要优化
			for(j=0;j<jumpoffset.length;j++)
			{
				if(search_index(listcode,offset_position+jumpoffset[j])>index_return)
				{
					branch_index.add(search_index(listcode,offset_position+jumpoffset[j]));
				    branch_indexnum.add(j);//要修改的branch序号
				}

			}
			//没有需要优化的 抛出
			if(branch_index.size()<=0) throw new Exception("do not need opt");
		///switch语句结束 转向语句的index
			//offsetmax
	/*		for(j=0;j<jumpoffset.length;j++)
			{
				if(jumpoffset[j]>=offsetmax)
				{
					offsetmax=jumpoffset[j];
					offsetmaxnum=j;
				}
			}
			*/
			//寻找最大的jump index
			index_max=0;
			for(j=0;j<branch_index.size();j++)
			{
				if(branch_index.get(j).intValue()>=index_max)
				{
					index_max=branch_index.get(j).intValue();
				}
			}
	   ////计算要插入的object个数
			//寻找 branch_end
			int index_forwardmax=index_max;
			 for(j=index_max;;j++)
		     {
				 //寻找最大的index_forward
				 if(j>index_forwardmax) index_forwardmax=j;
				 ////遇到branch结构式处理
				 if("BranchInstruction".equals(listcode.get(j).getClass().getSimpleName()))
				 {
					 
					 BranchInstruction branch2=(BranchInstruction)listcode.get(j);
					 if(!"goto".equals(branch2.getOpcodeVerbose()))//不算goto产生 的最大值
					 {
						 //寻找最大的index_forwardmax
						 if(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset())>index_forwardmax)
						 {
							 index_forwardmax=search_index(listcode, branch2.getOffset()+branch2.getBranchOffset());
						 }
					 }					 
					 ////退出条件//goto branchoffset<0 且 j>=index_forwardmax
					 if(("goto".equals(branch2.getOpcodeVerbose()))&&(branch2.getBranchOffset()<0)&&(j>=index_forwardmax))
					 break;
				 }
		     }
			 branch_endindex=index_forwardmax;
			 branch_endoffset=listcode.get(branch_endindex).getOffset();
			 BranchInstruction branch_temp=(BranchInstruction)listcode.get(index_forwardmax);
			 switch_endoffset=branch_temp.getBranchOffset()+branch_temp.getOffset();//switchcase 结束之后的第一条指令的offset
			 switch_endindex=search_index(listcode,switch_endoffset);
			 //branch_start
			 index_min=index_max;//先赋予最大值
				for(j=0;j<branch_index.size();j++)
				{
					if(branch_index.get(j).intValue()<=index_min)
						index_min=branch_index.get(j).intValue();
				}
				branch_startindex=index_min;
				branch_startoffset=listcode.get(index_min).getOffset();
				num=branch_endindex-branch_startindex+1;
				//记录branchstart-branchend 中要修改offset的BranchInstruction
				int k=0;
				for(j=branch_startindex;j<=branch_endindex;j++,k++)
				{
					 if("BranchInstruction".equals(listcode.get(j).getClass().getSimpleName()))
					 {
					BranchInstruction branch2=(BranchInstruction)listcode.get(j);
				     //记录后面要修改的branch这里只记录<index_branchstart
					 if(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset())<branch_startindex)
					 {
						 if(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset())>position)
						 {
					            
					            toend.add(k);
					            toendindex.add(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset()));
						 }
						 else
						 {
					     tostart.add(k);
						 tostartindex.add(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset()));
						 }
					 }
					 //记录后面要修改的branch这里只记录>index_branchend
					 if(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset())>branch_endindex)
					 {
                           tobigend.add(k);
                           tobigendindex.add(search_index(listcode, branch2.getOffset()+branch2.getBranchOffset()));
					 }
					 }
				}
	   ///插入
			AbstractInstruction[] preInstructions=null;
            //插入position后的指令
           AbstractInstruction[] postInstructions=new  AbstractInstruction[num];
           for(j=0;j<num;j++)//获取分支语句的指令
           {
           	postInstructions[j]=(AbstractInstruction)listcode.get(j+branch_startindex);           	
           }
             //计算offset——increase
           offset_increase=branch_endoffset-branch_startoffset;
           /////////////////////////////////////////
         //////////////////////插入branch语句
         //实例化一个 CodeInsertion
           CodeInsertion codeinsertion=new CodeInsertion(position,null,postInstructions,false);
           List <CodeInsertion>  codeinsertions=new ArrayList<CodeInsertion>();
           codeinsertions.add(codeinsertion);
           //调用CodeInsertion.apply方法 返回新的 instruction List
          List newlistcode= CodeInsertion.apply(listcode,codeinsertions,codeinfo);
          byte[] newcodes=ByteCodeWriter.writeByteCode(newlistcode);
          newlistcode=ByteCodeReader.readByteCode(newcodes);
       /////////////////////////////调整
          newswitch_index=switch_index;
          newdefault_index=default_index+num;
          newswitch_endindex=switch_endindex+num; 
          List <Integer> newbranch_index=new ArrayList<Integer>();
      //    int branch_adjnum=branch_index.get(offsetminnum)-default_index;
          int branch_adjnum=index_min-default_index;
          newbranch_endindex=branch_endindex-branch_adjnum;
          newbranch_startindex=branch_startindex-branch_adjnum;
          for(j=0;j<branch_index.size();j++)
          {
             newbranch_index.add(branch_index.get(j).intValue()-branch_adjnum);
          }
          //////////////修改switchcase\
          if(TSI_flag) 
          {
        	  TableSwitchInstruction newts_instruction=(TableSwitchInstruction)newlistcode.get(newswitch_index);
              AbstractInstruction temp=(AbstractInstruction)newlistcode.get(newdefault_index);
              int newdefaultoffset=temp.getOffset()-newts_instruction.getOffset();
              newts_instruction.setDefaultOffset(newdefaultoffset);
              ///修改jumpoffset
              int[] newjumpoffset=newts_instruction.getJumpOffsets();
              for(j=0;j<jumpoffset.length;j++)
              {
            	  //只修改需要修改的
            	  if(branch_indexnum.contains(j))
            	  {
            		  
                	  temp=(AbstractInstruction)newlistcode.get(newbranch_index.get(branch_indexnum.indexOf(j)).intValue());
                	  newjumpoffset[j]=temp.getOffset()-newts_instruction.getOffset();
            	  }

              }
              newts_instruction.setJumpOffsets(newjumpoffset);
              temp=(AbstractInstruction)newlistcode.get(newswitch_endindex);
              newswitch_endoffset=temp.getOffset();
          }
          if(LSI_flag)
          {
        	  LookupSwitchInstruction newls_instruction=(LookupSwitchInstruction)newlistcode.get(newswitch_index);
        	  AbstractInstruction temp=(AbstractInstruction)newlistcode.get(newdefault_index);
        	  int newdefaultoffset=temp.getOffset()-newls_instruction.getOffset();
        	  newls_instruction.setDefaultOffset(newdefaultoffset);
              ///修改jumpoffset
        	  List< MatchOffsetPair> newlsi_pair= newls_instruction.getMatchOffsetPairs(); //jump Pair
			  int [] newjumpoffset=new int[newlsi_pair.size()];  			  
			     for(int r=0;r<jumpoffset.length;r++)
			     {
			    	 newjumpoffset[r]=newlsi_pair.get(r).getOffset();
			     }
			     for(j=0;j<jumpoffset.length;j++)
	              {
	            	  //只修改需要修改的
	            	  if(branch_indexnum.contains(j))
	            	  {
	            		  
	                	  temp=(AbstractInstruction)newlistcode.get(newbranch_index.get(branch_indexnum.indexOf(j)).intValue());
	                	  newjumpoffset[j]=temp.getOffset()-newls_instruction.getOffset();
	            	  }

	              }
			     ///从 jumpoffset--->getMatchOffsetPairs
			     for(int r=0;r<newlsi_pair.size();r++)
			     {
			    	 newlsi_pair.get(r).setOffset(newjumpoffset[r]);
			     }
			     /////重新设置
			     newls_instruction.setMatchOffsetPairs(newlsi_pair);
	              temp=(AbstractInstruction)newlistcode.get(newswitch_endindex);
	              newswitch_endoffset=temp.getOffset();
          }
          
        /////修改branch中间语句
          //toend 
		     for( j=0;j<toend.size();j++)
           {
         	  int itemp=toend.get(j).intValue();
         	  int itemp2=toendindex.get(j).intValue();
         	 AbstractInstruction temp2=(AbstractInstruction)newlistcode.get(itemp2+num);
         	  BranchInstruction branch3=(BranchInstruction)newlistcode.get(newbranch_startindex+itemp);
         	  branch3.setBranchOffset(temp2.getOffset()-branch3.getOffset());
           }
		     //tostart
		     for( j=0;j<tostart.size();j++)
          {
        	  int itemp=tostart.get(j).intValue();
        	  int itemp2=tostartindex.get(j).intValue();
        	  AbstractInstruction temp2=(AbstractInstruction)newlistcode.get(itemp2);
        	  BranchInstruction branch3=(BranchInstruction)newlistcode.get(newbranch_startindex+itemp);
        	  branch3.setBranchOffset(temp2.getOffset()-branch3.getOffset());
          }
		     //tobigend
		     for(j=0;j<tobigend.size();j++)
		     {
           	  int itemp=tobigend.get(j).intValue();
            	  int itemp2=tobigendindex.get(j).intValue();
            	  AbstractInstruction temp2=(AbstractInstruction)newlistcode.get(itemp2+num);
            	  BranchInstruction branch3=(BranchInstruction)newlistcode.get(newbranch_startindex+itemp);
            	  branch3.setBranchOffset(temp2.getOffset()-branch3.getOffset()); 
		     }
		     tostart.clear();
		     toend.clear();
		     tobigend.clear();
		     tostartindex.clear();
		     toendindex.clear();
		     tobigendindex.clear();
          ///修改原branch为 nop
          SimpleInstruction temp3=new SimpleInstruction(0x00);
          temp3.setOpcode(0x00);
          //删除
          for(j=0;j<num;j++)
          {
        	  newlistcode.remove(branch_startindex+num);
          }
          //增加
          for(j=0;j<=(offset_increase+2);j++)//最后的goto占3个字节
          {
        	  newlistcode.add(branch_startindex+num, temp3);
          }
          ///检测有没有错误产生
          for( j=0;j<newlistcode.size();j++)
          {
          	if("BranchInstruction".equals(newlistcode.get(j).getClass().getSimpleName()))
          	{
              	  BranchInstruction checktemp=(BranchInstruction)newlistcode.get(j);
                	  int checkindex_temp=search_index(newlistcode,checktemp.getBranchOffset()+checktemp.getOffset());
                	  AbstractInstruction checktemp2=(AbstractInstruction)newlistcode.get(checkindex_temp);
                	  if(checktemp2.getOpcode()==0x00) throw  new Exception("miss lable ");
          	}
          } 
          ////////////新的 code写回method
          byte[] newcodes2=ByteCodeWriter.writeByteCode(newlistcode);
          newlistcode=ByteCodeReader.readByteCode(newcodes2);
          listcode=newlistcode;//为后面继续处理做准备
          opreturn=(AbstractInstruction)listcode.get(0);
          for(int n=0;n<listcode.size();n++)
           {
          	 opreturn=(AbstractInstruction)listcode.get(n);
          	 if((opreturn.getOpcode()==0xac)||(opreturn.getOpcode()==0xad)||(opreturn.getOpcode()==0xae)||(opreturn.getOpcode()==0xaf)||(opreturn.getOpcode()==0xb0)||(opreturn.getOpcode()==0xb1))
          	 {
          		 index_return=n;
          		 offset_return=opreturn.getOffset();
          	 }
           }
          codeinfo.setCode(newcodes2);
	       //删除 exceptiontable
	       codeinfo.setExceptionTable(null);
           
		}
	}
	 //fis.close();
	}
    catch (Exception e)
    {
    	
   	   String s1="\n switch-case optimization    "+methods[methodnum].getName()+" method failed \n";
      	System.out.println(s1);
    }
}
public static int search_index(List<AbstractInstruction> listcode,int offset)//通过offset查找index
{
	for(int i=0;i<listcode.size();i++)
	{
		if(listcode.get(i).getOffset()==offset)
		return i;
	}
	return -1;
}
public static int search_end()//查找branch语句的起始end index
{
	
	return -1;
}
 /*   public static void main(String args[])      
{       
    	 List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\swtest4\\cn\\m\\xys","class",true);
	    //	List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\swtest5\\com\\example\\antiy_dex","class",true);
	    //	List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\swtest6\\com\\example\\whsw_test","class",true);
	    //	List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\whtest3\\com\\yarin\\android\\FileManager","class",true);	
	    //	List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\whtest2\\com\\xmobileapp\\rockplayer","class",true);
	    //	List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\whtest1\\cn\\hy\\mygame","class",true);
	    //	List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test7\\com\\yarin\\android\\CityWeather","class",true);
	    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test8\\com\\yarin\\android\\MobileMap","class",true);
	    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test9\\com\\yarin\\android\\MyContacts","class",true);
	    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test10\\com\\yarin\\android\\RingProfile","class",true);
	    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test11\\com\\yarin\\android\\TodayDate","class",true);
	    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test12\\com\\cola\\ui","class",true);
	    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test13\\com\\yarin\\android\\MusicPlayer","class",true);
    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test14\\com\\android","class",true);
    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test15\\com\\china\\gltry","class",true);
    	 //List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test16\\com\\example\\myfish","class",true);
    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test17\\com\\geolo\\android","class",true);
    	//List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test18\\com\\ostrichmyself\\txtReader","class",true);
    	 //List fileList =getListFiles("D:\\dex2jar-0.0.9.8\\test19\\yzy\\Tank","class",true);
    	//	List filelist=getListFiles(args[0].toString(),"class",true);
    	for(int i=0;i<fileList.size();i++)
    	{
    		if(fileList.get(i).toString().contains("R$")) continue;
                     		Run(fileList.get(i).toString()); 
    	}//
    	//Run(args[0].toString());
    	//Run("D:\\android_ana_tool\\dex2jar-0.0.9.12\\wh1\\cn\\hy\\mygame\\view\\GameView.class");
    	System.out.println("all files finish!");
}  */  

    /**
    * 
    * @param path 文件路径
    * @param suffix 后缀名
    * @param isdepth 是否遍历子目录
    * @return
    */
    public static List getListFiles(String path, String suffix, boolean isdepth) 
    {
       File file = new File(path);
       return listFile(file ,suffix, isdepth);
    }

    public static List listFile(File f, String suffix, boolean isdepth) 
    {
       //是目录，同时需要遍历子目录
       if (f.isDirectory() && isdepth == true)
       {
        File[] t = f.listFiles();
        for (int i = 0; i < t.length; i++)
        {
         listFile(t[i], suffix, isdepth);
        }
       }
       else 
       {
        String filePath = f.getAbsolutePath();
       
        if(suffix !=null)
        {
         int begIndex = filePath.lastIndexOf(".");//最后一个.(即后缀名前面的.)的索引
         String tempsuffix = "";
        
         if(begIndex != -1)//防止是文件但却没有后缀名结束的文件
         {
          tempsuffix = filePath.substring(begIndex + 1, filePath.length());
         }
        
         if(tempsuffix.equals(suffix))
         {
          fileList.add(filePath);
         }
        }
        else
        {
         //后缀名为null则为所有文件
         fileList.add(filePath);
        }
       
       }
      
       return fileList;
    }

    /**
        * 方法追加文件：使用FileWriter
        * @param fileName
        * @param content
        */
    public static void appendMethod(String fileName, String content)
    {
        try 
        {
         //打开一个写文件器，构造函数中的第二个参数true表示以追加形式写文件
         FileWriter writer = new FileWriter(fileName, true);
         writer.write(content + "/r/n");
         writer.close();
        } 
        catch (IOException e) 
        {
         e.printStackTrace();
        }
    } 
}
